﻿''' <summary>
''' This class implements a GAC collector.
''' </summary>
''' <remarks>
''' The GACCollector's purpose is to read all registry entries of
''' HKLM/Software/Microsoft/Fusion/GACChangeNotification/Default
''' that holds the contents of the global assembly cache.
''' </remarks>
Public Class GACCollector
    Implements ICollector

    ''' <summary>
    ''' List of writers passed when Startup() is called.
    ''' </summary>
    ''' <remarks></remarks>
    Protected m_writers As List(Of Writer.IWriter)

    ''' <summary>
    ''' This holds an error description.
    ''' </summary>
    ''' <remarks></remarks>
    Protected m_error As String

    Public Sub Cleanup() Implements ICollector.Cleanup

        'Nothing to do 

    End Sub

    Public Sub Run() Implements ICollector.Run

        'Write our category name.
        Writer.WritersHelper.WriteCategory("GAC Information", m_writers)
        Writer.WritersHelper.WriteSection("Assemblies in GAC", m_writers)

        'If an error occurred while reading the configuration file, write
        'it out and stop here.
        If m_error <> "" Then
            Writer.WritersHelper.WriteCategoryError(m_error, m_writers)
        Else

            Try
                'Get all keys of Fusion.
                Dim rk As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\Microsoft\Fusion\GACChangeNotification\Default")
                Dim names() As String = rk.GetValueNames

                'Put'em all in a list.
                Dim sl As New List(Of GACEntry)
                For Each s As String In names
                    Dim sSplit() As String = s.Split(","c)
                    If sSplit.GetLength(0) >= 3 Then
                        Dim ge As New GACEntry(sSplit(0), sSplit(1), sSplit(3))
                        sl.Add(ge)
                    End If
                Next

                'Sort the list by name/version.
                sl.Sort()

                'Write them out.
                For Each ge As GACEntry In sl
                    Writer.WritersHelper.WriteEntry(ge.Name.PadRight(65, " "c), "version=" + ge.Version + ", key=" + ge.Key, m_writers)
                Next
            Catch ex As Exception
                'Error gathering GAC info.
                Writer.WritersHelper.WriteSectionError("Error gathering GAC info.", m_writers)
            End Try

        End If

    End Sub

    Public Sub Startup(ByVal cfgFile As String, ByVal writers As System.Collections.Generic.List(Of Writer.IWriter)) Implements ICollector.Startup

        'Get all writers.
        m_writers = writers

    End Sub

End Class
